﻿@namespace MudBlazor.UnitTests.TestComponents
@using System.Globalization;

<MudDataGrid @ref="DataGrid" Items="@_items" Sortable="true">
    <Columns>
        <PropertyColumn Property="x => x.Name" Comparer="@(NaturalSortingEnabled ? new MudBlazor.Utilities.NaturalComparer() : null)" />
        <PropertyColumn Property="x => x.Value" />
        <PropertyColumn Property="x => x.Misc" />
    </Columns>
</MudDataGrid>
<MudButton OnClick="CallSort" Variant="Variant.Outlined">Set Sort on Name Column</MudButton>
<MudSwitch @bind-Checked="@NaturalSortingEnabled">Enable Natural Sorting</MudSwitch>

@code {
    public static string __description__ = @"Test in browser custom sorting of DataGrid. Note that the 'Name' column is being sorted using a natural sorting algorithm.";
    private MudDataGrid<Item> DataGrid;

    private bool NaturalSortingEnabled = true;

    private IEnumerable<Item> _items = new List<Item>()
    {
        new Item("1", 42, "555"), 
        new Item("10", 73, "7"), 
        new Item("2", 11, "4444"), 
        new Item("1_10", 33, "33333"),
        new Item("1_2", 99, "66"), 
        new Item("1_11", 44, "1111111"),
        new Item("0", 55, "222222")
    };
   

    private async Task CallSort()
    {
        await DataGrid.SetSortAsync(nameof(Item.Name), SortDirection.Ascending, x => x.Name, new MudBlazor.Utilities.NaturalComparer());
    }

    public class Item
    {
        public string Name { get; set; }
        public int Value { get; set; }
        public string Misc { get; set; }

        public Item(string name, int value, String misc)
        {
            Name = name;
            Value = value;
            Misc = misc;
        }
    }
}
